home *** CD-ROM | disk | FTP | other *** search
-
-
-
- GETOPT(3) MINTLIB LIBRARY FUNCTIONS GETOPT(3)
-
-
- N✓NA✓AM✓ME✓E
- getopt - get option letter from argument vector
-
- S✓SY✓YN✓NO✓OP✓PS✓SI✓IS✓S
- #include <unistd.h>
-
- int getopt(int argv, char * const *argv, const char *optstring);
-
- extern char *optarg;
-
- extern int optind;
-
- extern int opterr;
-
- D✓DE✓ES✓SC✓CR✓RI✓IP✓PT✓TI✓IO✓ON✓N
- getopt returns the next option letter in argv that matches
- a letter in optstring. It supports all the rules of the
- UN*X System V command syntax standard (see the section
- RULES below). All new programs that wish to adhere to the
- command syntax standard should use getopt to parse posi-
- tional parameters and check for options that are legal for
- that command.
-
- optstring must contain the option letters the command
- using getopt will recognize; if a letter is followed by a
- colon, the option is expected to have an argument, or
- group of arguments, which must be separated from it by
- white space.
-
- optarg is set to point to the start of the option-argument
- on return from getopt.
-
- getopt places in optind the argv index of the next argu-
- ment to be processed. optind is external and is initial-
- ized to 1 before the first call to getopt.
-
- When all options have been processed (i.e., up to the
- first non-option argument), getopt returns EOF. The spe-
- cial option "--" may be used to delimit the end of the
- options; when it is encountered, EOF will be returned, and
- "--" will be skipped.
-
- D✓DI✓IA✓AG✓GN✓NO✓OS✓ST✓TI✓IC✓CS✓S
- getopt prints an error message on standard error and
- returns a question mark (?) when it encounters an option
- letter not included in optstring or no option-argument
- after an option that expects one. This error message may
- be disabled by setting opterr to 0.
-
- R✓RU✓UL✓LE✓ES✓S
- The UN*X System V command standard contains the following
- rules:
-
- 1. Command names must be between two and nine characters
-
-
-
- MiNT docs 0.1 3 March 1993 1
-
-
-
-
-
- GETOPT(3) MINTLIB LIBRARY FUNCTIONS GETOPT(3)
-
-
- long.
- 2. Command names must include only lower-case letters and
- digits.
- 3. Option names must be one character long.
- 4. All options must be preceded by "-".
- 5. Options with no arguments may be grouped with a single
- "-".
- 6. The first option-argument following an option must be
- preceded by white space.
- 7. Option-arguments cannot be optional.
- 8. Groups of option-arguments following an option must be
- either
- be separated by commas or separated by white space and
- quoted
- (e.g., -o xxx,z,yy or -o "xxx z yy").
- 9. All options must precede operands on the command line.
- 10. "--" may be used to indicate the end of the options.
- 11. The order of the options relative to another should
- not matter. 12. The relative order of the operands may
- effect their
- significance in ways determined by the command with
- which
- they appear. 13. "-" preceded and followed by white
- space should only be used
- to mean standard input.
-
- getopt supports rules 3-10 above; the enforcement of the
- other rules must be done by the command itself.
-
- E✓EX✓XA✓AM✓MP✓PL✓LE✓E
- The following code fragment shows how one might process the
- arguments for a command that can take the mutually exclusive
- options 'a' and 'b', and the option 'o', which requires an
- option-argument.
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- extern char *optarg;
-
- void main(int argc, char *argv[])
- {
- int c, aflg = 0, bflg = 0, errflg = 0;
- char *ofile = NULL;
-
- while ((c = getopt(argc, argv, "abo:")) != EOF)
- switch (c)
- {
- case 'a':
- if (bflg != 0)
- errflg++;
- else
- aflg++;
- break;
-
-
-
- MiNT docs 0.1 3 March 1993 2
-
-
-
-
-
- GETOPT(3) MINTLIB LIBRARY FUNCTIONS GETOPT(3)
-
-
- case 'b':
- if (aflg != 0)
- errflg++;
- else
- bflg++;
- break;
- case 'o':
- ofile = optarg;
- break;
- case '?':
- errflg++;
- }
- if (errflg != 0)
- {
- fprintf(stderr, "Usage:...n");
- exit(1);
- }
- ...
- }
-
- N✓NO✓OT✓TE✓ES✓S
- getopt cannot be used for complicated context-sensitive
- argument vector parsing.
-
- The UN*X System V standard may seem too restrictive; for
- instance, in the above example, '-ofile' is not allowed
- ('-o file' is).
-
- Some systems return -1 instead of EOF; this may actually
- make a difference on some systems.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- MiNT docs 0.1 3 March 1993 3
-
-
-